<?php

namespace app\company\controller\rating_scale\general;

use app\admin\service\RatingScaleImportService;
use app\BaseController;
use app\model\RatingScaleModel;
use app\model\UserModel;
use think\App;

class RatingScale extends BaseController
{
    protected $mode;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new RatingScaleModel();
    }

    /**
     * 列表
     * @return bool|string
     */
    public function list(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])){
                $where[] = ['name','like','%'.$param['name'].'%'];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            }else{
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['level'])){
                $UserModel = new UserModel();
                $uid = $UserModel->where('level','=',$param['level'])->column('id');
                if (count($uid) > 0){
                    $where[] = ['uid','in',$uid];
                }else{
                    $where[] = ['id','=',-1];
                }
            }
            if (isset($param['use_price']) && strlen($param['use_price']) > 0){
                if ($param['use_price'] == 1){
                    $where[] = ['use_price','>',0];
                }else{
                    $where[] = ['use_price','=',0];
                }
            }
            if (isset($param['use_price']) && strlen($param['interpret_price']) > 0){
                if ($param['interpret_price'] == 1){
                    $where[] = ['interpret_price','>',0];
                }else{
                    $where[] = ['interpret_price','=',0];
                }
            }
            $where[] = ['company_id','=',$this->userData['company_id']];
//            $where[] = ['areas_id','=',$this->userData['areas_id']];
            $where[] = ['channel','=',3];
            $query = $this->mode->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 新增量表
     * @return false|string
     */
    public function add(): bool|string
    {
        $this->mode->startTrans();
        try {
            $param = $this->request->param('data');
            if (empty($param)) throw new \Exception('参数错误！');
            $nameArr = array_column($param, 'name');
            if (count($nameArr) > 0){
                $is_repeat = $this->mode->where([
                    ['company_id','=',$this->userData['company_id']],
                    ['channel','=',3],
                    ['name', 'in', $nameArr]
                ])->field('id,name')->find();
                if ($is_repeat) throw new \Exception($is_repeat['name'].'量表名称重复！');
            }
            $RatingScaleImportService = new RatingScaleImportService($this->app);
            foreach ($param as $item) {
                if (!$RatingScaleImportService->import($item['file'], [
                    'company_id' => $this->userData['company_id'],
                    'areas_id' => $this->userData['areas_id'],
                    'uid' => $this->userData['id'],
                    'name' => $item['name'],
                    'cover' => $item['cover']??'',
                    'use_price' => $item['use_price']??0,
                    'interpret_price' => $item['interpret_price']??0,
                ],3)) throw new \Exception($RatingScaleImportService->message.'line:'.$RatingScaleImportService->line);
            }
            $this->mode->commit();
            return success([],'添加成功！');
        } catch (\Exception $e) {
            $this->mode->rollback();
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}